<?php

namespace Module;

class ModuleManager {
	private $root;

	private static function log ($what) {
		echo('[ModuleManager]: '.$what."\n");
	}
	
	public function __construct ($root) {
		assert(is_string($root));

		$this->root = $root;

		spl_autoload_register( function ($name) {
			$path = $this->root.'/'.implode('/', explode('\\', $name)).'.php';
			$this->log("autoloading $name from $path");

			{ require_once $path; }
			
			$class = new \ReflectionClass($name);
			if ($class->hasMethod('initialise'))
				$name::initialise();
		});
	}

	public function __destruct () {
		$this->log('No more ModuleManager');
	}

	public function load ($who) {
		spl_autoload_call($who);
	}
};

